package com.ddwl.goods.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.goods.service.ShoppingCartService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.ShoppingSkuVo;
import com.ddwl.schema.bo.goods.WbmShoppingSkuBo;
import com.ddwl.schema.bo.goods.retail.ShoppingCartPageBo;
import com.ddwl.schema.bo.goods.retail.ShoppingCartPageByShopBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.ShoppingCartListVo;
import com.ddwl.schema.vo.goods.ShoppingCartVo;
import com.ddwl.schema.vo.goods.retail.ShoppingCartPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 类ShoppingCardController.java的实现描述：购物车相关接口
 *
 * @author zach
 * @Date :2020-06-08 17:12.
 */

@Api(tags = "购物车相关接口")
@RequestMapping("shoppingCard")
@RestController
public class ShoppingCardController extends BaseController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("保存购物车商品信息")
    public RestVo save(@Valid @RequestBody ShoppingSkuVo shoppingSkuVo) {
        shoppingCartService.userSaveShoppingCart(shoppingSkuVo, getUserId(),Boolean.TRUE);
        return RestVo.ok();
    }

    @PostMapping("/wbm/save")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序保存购物车商品信息")
    public RestVo wbmSave(@Valid @RequestBody WbmShoppingSkuBo wbmShoppingSkuBo) {
        shoppingCartService.wbmSaveShoppingCar(wbmShoppingSkuBo, getUserId());
        return RestVo.ok();
    }

    @DeleteMapping
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商品移除购物车")
    public RestVo delete(@ApiParam("skuId多个逗号隔开") @RequestParam("skuCodes") String skuCodes) {
        shoppingCartService.userRemoveShoppingCart(Arrays.asList(skuCodes.split(",")), getUserId());
        return RestVo.ok();
    }

    @DeleteMapping("clear")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("清空用户购物车")
    public RestVo clear() {
        shoppingCartService.userClearShoppingCart(getUserId());
        return RestVo.ok();
    }

    @GetMapping
    @Auth(product = {BaseEnum.ProductEnum.MALL})
    @ApiOperation("获取用户购物车列表")
    public RestVo<ShoppingCartListVo> get() {
        return RestVo.ok(shoppingCartService.queryClearShoppingCartPage(getUserId()));
    }

    @DeleteMapping("/{userId}")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.API,BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("商品移除购物车")
    public RestVo delete(@RequestParam("skuCodes") List<String> skuCodes,@PathVariable("userId") String userId) {
        shoppingCartService.userRemoveShoppingCart(skuCodes, userId);
        return RestVo.ok();
    }

    @PostMapping("/wbm/page")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序用户购物车按店铺分页")
    public RestVo<PageVo<ShoppingCartPageVo>> page(@Valid @RequestBody PageBo<ShoppingCartPageBo> pageBo) {
        return RestVo.ok(shoppingCartService.wbmPage(pageBo,getUserId()));
    }

    @GetMapping("/wbm/count")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE,BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("获取购物车商品数量")
    public RestVo<Integer> count() {
        return RestVo.ok(shoppingCartService.count(getUserId()));
    }

    @PostMapping("/wbm/pageByShop")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序用户购物车店铺下商品分页")
    public RestVo<PageVo<ShoppingCartVo>> pageByShop(@Valid @RequestBody PageBo<ShoppingCartPageByShopBo> pageBo) {
        pageBo.getParam().setUserId(getUserId());
        return RestVo.ok(shoppingCartService.pageByShop(pageBo));
    }

}
